
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 责任链模式
 *
 * @author liouwb
 */
public class ChainPatternTest {
    public static void main(String[] args) {
        // 面试者
        Applicant applicant = new Applicant("liouwb", 28, "java");
        // 面试结果
        Result result = new Result();
        result.setFlag(true);

        // 面试流程链 添加面试流程 可以自定义调整
        InterviewChain interviewChain = new InterviewChain();
        interviewChain.add(new InterviewiWritten())
                .add(new InterviewerLeader())
                .add(new IntervewerManager())
                .add(new IntervewerHr());
        // 面试
        interviewChain.interview(applicant, result);
    }

}

/**
 * 面试流程链条
 */
class InterviewChain implements Interviewer {
    List<Interviewer> chains = new CopyOnWriteArrayList<>();

    /**
     * @param interviewer
     * @return this 可以采用链式编程
     */
    InterviewChain add(Interviewer interviewer) {
        chains.add(interviewer);

        return this;
    }

    /**
     * 面试流程链同样实现面试流程接口
     *
     * @param applicant
     * @param result
     * @return
     */
    @Override
    public boolean interview(Applicant applicant, Result result) {
        for (Interviewer interviewer : chains) {
            if (!interviewer.interview(applicant, result)) {
                result.setFlag(false);

                return result.isFlag();
            }
        }
        return true;
    }
}

/**
 * 抽象出来面试官类
 */
interface Interviewer {
    /**
     * 面试
     *
     * @return 返回面试结果
     */
    boolean interview(Applicant applicant, Result result);
}

/**
 * 应聘者
 */
class Applicant {
    // 姓名
    protected String name;

    // 年龄
    protected int age;

    // 应聘岗位
    protected String job;

    public Applicant(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

/**
 * 面试结果
 */
class Result {
    // 面试结果
    protected String result = "";

    // 是否通过
    protected boolean flag;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}

/**
 * 笔试面试环节
 */
class InterviewiWritten implements Interviewer {
    @Override
    public boolean interview(Applicant applicant, Result result) {
        System.out.println(applicant.getName() + "同学，进入笔试面试环节，应聘的岗位为：" + applicant.getJob());

        result.setResult(result.getResult() + "笔试面试通过");
        System.out.println(result.getResult());
        result.setFlag(true);

        return result.isFlag();
    }
}

/**
 * 技术首面
 */
class InterviewerLeader implements Interviewer {
    @Override
    public boolean interview(Applicant applicant, Result result) {
        System.out.println(applicant.getName() + "同学，进入首轮技术面试环节，应聘的岗位为：" + applicant.getJob());

        result.setResult(result.getResult() + "-->技术首面通过");
        System.out.println(result.getResult());
        result.setFlag(true);

        return result.isFlag();
    }
}

/**
 * 技术经理二面
 */
class IntervewerManager implements Interviewer {

    @Override
    public boolean interview(Applicant applicant, Result result) {
        System.out.println(applicant.getName() + "同学，进入二轮技术面试环节，应聘的岗位为：" + applicant.getJob());

        result.setResult(result.getResult() + "-->技术二轮面试通过");
        System.out.println(result.getResult());
        result.setFlag(true);

        return result.isFlag();
    }
}

/**
 * 人资终面
 */
class IntervewerHr implements Interviewer {

    @Override
    public boolean interview(Applicant applicant, Result result) {
        System.out.println(applicant.getName() + "同学，进入人资面试环节，应聘的岗位为：" + applicant.getJob());

        result.setResult(result.getResult() + "-->人资终面通过");
        System.out.println(result.getResult());
        result.setFlag(true);

        return result.isFlag();
    }
}


